﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace GameBoard
{
    public class ObjectVisual : ModelVisual3D, IVisual, ITerrainFeature
    {
        bool _belongsToMoveablePicture = false;
        public bool BelongsToMoveablePicture
        {
            get { return _belongsToMoveablePicture; }
        }
        public bool IsSolid
        {
            get;
            set;
        }
        public void PopulateVisualData(BaseVisualData theData)
        {
            _belongsToMoveablePicture = theData.BelongsToMoveablePicture;
            FactoryID = theData.FactoryID;

            var objData = theData as ObjectVisualData;
            IsSolid = !objData.IsNotSolid;
            Content = MakeModelVisual3DFromObjectFile(objData.ObjectFile, objData.Material).Content;
            Transform3DGroup t = new Transform3DGroup();
            t.Children.Add(objData.RotateTransform);
            t.Children.Add(objData.ScaleTransform);
            t.Children.Add(objData.TranslateTransform);
            Transform = t;
        }

        public ModelVisual3D VisualElement
        {
            get { return this; }
        }


        private ModelVisual3D MakeModelVisual3DFromObjectFile(string modelPath, Material frontMaterial)
        {
            Model3DGroup group = HelixToolkit.Wpf.ModelImporter.Load(modelPath);
            ModelVisual3D myModelVisual3D = new ModelVisual3D();
            myModelVisual3D.Content = group;
            foreach (GeometryModel3D geometry in (myModelVisual3D.Content as Model3DGroup).Children)
            {
                geometry.Material = frontMaterial;
            }
            var modelContent = myModelVisual3D.Content as GeometryModel3D;
            return myModelVisual3D;
        }


        public Guid FactoryID
        {
            get;
            set;
        }
    }
}
